/*
 * Actuator.java
 *
 * Created on June 16, 2007, 8:49 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package atag.runtime.actuators;

//import com.sun.spot.sensorboard.IDemoBoard;
//import com.sun.spot.sensorboard.EDemoBoard;
//import com.sun.spot.sensorboard.capabilities.ITriColorLEDController;
//import com.sun.spot.sensorboard.peripheral.ITriColorLED;
//import com.sun.spot.sensorboard.peripheral.LEDColor;
//import java.io.IOException;

/**
 *
 * @author Sirius
 */
public class Actuator implements ATaGActuator {
    
    private IDemoBoard sensorBoard;
    private ITriColorLED[] LEDs;
    private final int NUM_LED_TO_DISPLAY_VALUE = 6; // use 6 leds to indicate output values, maximum is 7, the first led is used for other use
    //private int leds
    /** Creates a new instance of Actuator */
    
    private int maxValueLED;
    private int currentValueLED;
    private boolean isLEDEnable;
            
    public Actuator() {
        this.sensorBoard = EDemoBoard.getInstance();
        this.LEDs = sensorBoard.getLEDs();
        this.maxValueLED = 0;
        this.currentValueLED = 0;
        this.isLEDEnable = true;
        this.initializeLED();
    }
    
    private void initializeLED() {  
        for (int i = 0; i < 8; i++) {
            LEDs[i].setOff();			// turn off all LEDs
        }
        LEDs[0].setOn();
        LEDs[0].setColor(LEDColor.GREEN);
    }
    
    public void setMaxValueLED(int maxValue) {
        this.maxValueLED = maxValue;
        if (this.isLEDEnable = true) {
            int temp_numLED = this.calculateScaleLED();
            this.setLEDs(temp_numLED);
        }
    }
    
    public void setCurrentValueLED(int newValue) {
        this.maxValueLED = newValue;
        if (this.isLEDEnable = true) {
            int temp_numLED = this.calculateScaleLED();
            this.setLEDs(temp_numLED);
        }
    }
    
    public void disableLEDs() {
        this.isLEDEnable = false;
    }
    
    public void enableLEDs() {
        this.isLEDEnable = true;
    }
    
    private int calculateScaleLED () {
        int num_LED;
        int temp_currentValue = this.currentValueLED;
        int temp_maxValue = this.maxValueLED;
        if (temp_maxValue <= 0) {
            num_LED = 0;
        }
        else {
            if (temp_currentValue > temp_maxValue) {
                temp_currentValue = temp_maxValue;
            }
            num_LED = (int) this.NUM_LED_TO_DISPLAY_VALUE*(temp_currentValue/temp_maxValue);
        }
        return num_LED;
    }
    
    private void setLEDs(int num_LED) {
        
        if (num_LED == 0) {
             for (int i = 8 - this.NUM_LED_TO_DISPLAY_VALUE; i < 8; i++) {
                LEDs[i].setOff();			// turn off all display LEDs
             }
        }
        else {
             for (int i = 8 - this.NUM_LED_TO_DISPLAY_VALUE; i < 8; i++) {
                LEDs[i].setOff();			// turn off all LEDs
             }
             for (int i = 8 - num_LED; i < 8; i++) {
                    LEDs[i].setOn();   
                    LEDs[i].setColor(LEDColor.BLUE);
                    //Utils.sleep(200);
                    //leds[i].setOff();
                    //Utils.sleep(200);
             }
        }
    }
}
